
/* GCSx
** WORLD.H
**
** World storage
** Non-editor functionality
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_WORLD_H_
#define __GCSx_WORLD_H_

class World : virtual public LoadOnly {
public:
    typedef std::map<int, class Scene*> SceneIndex;
    typedef hash_map<const char*, class Scene*, hash<const char*>, hash_eqstr> SceneMap;
    typedef std::map<int, class TileSet*> TileSetIndex;
    typedef hash_map<const char*, class TileSet*, hash<const char*>, hash_eqstr> TileSetMap;
    typedef std::map<int, class AnimGroup*> AnimGroupIndex;
    typedef hash_map<const char*, class AnimGroup*, hash<const char*>, hash_eqstr> AnimGroupMap;
    typedef std::map<int, class Script*> ScriptIndex;
    typedef hash_map<const char*, class Script*, hash<const char*>, hash_eqstr> ScriptMap;
    typedef hash_map<const char*, std::pair<int,const char*>, hash<const char*>, hash_eqstr> GlobalMap;
    
protected:
    // World title
    std::string title;
    
    // Our file
    std::string* filename;
    class WorldFileLoad* file;
    
    // Our resources, indexed by lowercase name and by id
    SceneIndex scenesById;
    SceneMap scenesByName;
    TileSetIndex tilesetsById;
    TileSetMap tilesetsByName;
    AnimGroupIndex animgroupsById;
    AnimGroupMap animgroupsByName;
    ScriptIndex scriptsById;
    ScriptMap scriptsByNameCode;
    ScriptMap scriptsByNameLib;
    ScriptMap scriptsByNameNote;
    
    // Used for linking during gameplay, and for verifying global variable
    // consistency across scripts; should not be cleared during gameplay.
    // (char pointers are allocated specifically to place in this map)
    GlobalMap globalLinks;
    int globalLinksDone;
    int globalLinksCount;
    void clearGlobalLinks();
    // Returns # of errors; assumes all scripts are compiled
    int prepGlobalLinks();

    // World properties
    int startingScene; // (id number)

    // Used by WorldEdit
    World();

    // Deletes all objects in our resources; used in destructor or failed construct
    // Safe to call multiple times, but leaves World object in a state unusable except
    // for further destruction/cleanup
    void deleteCollections();
    
public:
    World(const char* wFilename) throw_File; // Opens an existing world
    virtual ~World();
    
    // Accessors
    const std::string& getTitle() const { return title; }
    int getStartScene() const { return startingScene; }
    int getId() const { return 0; }
    int getBlockType() const { return WorldFileLoad::BLOCKTYPE_WORLDINFO; }
        
    // Add and remove from byId and byName indices
    // DO NOT CHANGE RESOURCE NAME WITHOUT REMOVING FIRST
    // (to change name, remove and readd)
    virtual void indexScene(Scene* addScene);
    virtual void deindexScene(Scene* remScene);
    void indexTileSet(TileSet* addTileSet);
    void deindexTileSet(TileSet* remTileSet);
    void indexAnimGroup(AnimGroup* addAnimGroup);
    void deindexAnimGroup(AnimGroup* remAnimGroup);
    void indexScript(Script* addScript);
    void deindexScript(Script* remScript);
    
    // Find items based on name/id or iterate sequence
    // Designed for runtime efficiency
    class Scene* findScene(const std::string& fName) const;
    class Scene* findScene(int fId) const;
    SceneIndex::const_iterator beginScene() const { return scenesById.begin(); }
    SceneIndex::const_iterator endScene() const { return scenesById.end(); }
    
    class TileSet* findTileSet(const std::string& fName) const;
    class TileSet* findTileSet(int fId) const;
    TileSetIndex::const_iterator beginTileSet() const { return tilesetsById.begin(); }
    TileSetIndex::const_iterator endTileSet() const { return tilesetsById.end(); }
    
    class AnimGroup* findAnimGroup(const std::string& fName) const;
    class AnimGroup* findAnimGroup(int fId) const;
    AnimGroupIndex::const_iterator beginAnimGroup() const { return animgroupsById.begin(); }
    AnimGroupIndex::const_iterator endAnimGroup() const { return animgroupsById.end(); }
    
    class Script* findScriptCode(const std::string& fName) const;
    class Script* findScriptLib(const std::string& fName) const;
    class Script* findScriptNote(const std::string& fName) const;
    class Script* findScript(int fId) const;
    ScriptIndex::const_iterator beginScript() const { return scriptsById.begin(); }
    ScriptIndex::const_iterator endScript() const { return scriptsById.end(); }
    
    // Used both during editing and gameplay
    // Returns # of errors
    int buildScripts();

    // File access
    void loadHeader(class FileRead* file) throw_File;
    virtual void loadContent(class FileRead* file);
    int isContentCached() const;
    void cacheLoad();
    int markLock();
    int markUnlock();
    int isLocked() const;
};

#endif

